var app = getApp()
var promise = require('../../utils/request.js');
var util = require('../../utils/util.js');

Page({
  data: {
    results: [],
    showModal: false,
    removrBtn: false
  },
  addAccount: function () {
    wx.navigateTo({
      url: '../add/add',
    })
  },
  onLoad: function (options) {
    var that = this;
    var unionId = wx.getStorageSync('unionId');
    promise.sendRequest('account/list/' + unionId, 'Get', '', null, 4, {
      success: function (data) {
        console.log(data)
        if (data) {
          for (var i = 0; i < data.length; i++) {
            var createTime = util.formatTimeToDay(data[i].createTime)
            var timeLimit = util.formatTimeToDay(data[i].timeLimit)
            data[i].createTime = createTime;
            data[i].timeLimit = timeLimit;
          }
          that.setData({
            results: data
          })
        }
      }
    }, {
        fail: function (res) {
          promise.showModule(res);
        }
      })
  },
  toDetail: function (event) {
    // console.log(event)
    let that = this
    var id = event.currentTarget.dataset.item.id;
    var code = event.currentTarget.dataset.item.code;
    var unionId = wx.getStorageSync('unionId');
    //触摸时间距离页面打开的毫秒数  
    var touchTime = that.data.touch_end - that.data.touch_start
    if (touchTime > 350) {
      wx.showModal({
        title: '提示',
        content: '是否删除账套',
        success(res) {
          if (res.confirm) {
              promise.sendRequest('/account/' + code + '/' + unionId, 'DELETE', '', null, 4, {
                success: function (data) {
                  if (data) {
                    console.log(data)
                    
                  } else {

                  }
                  console.log();
                }
              }, {
                  fail: function (res) {
                    promise.showModule(res);
                  }
                })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.navigateTo({
        url: '../index/index?id=' + id,
      })
    }
  },
  //按下事件开始
  mytouchstart(e) {
    let that = this;
    that.setData({
      touch_start: e.timeStamp
    })
    // console.log(e.timeStamp + '- touch-start')
  },
  //按下事件结束  
  mytouchend(e) {
    let that = this;
    that.setData({
      touch_end: e.timeStamp
    })
    // console.log(e.timeStamp + '- touch-end')
  },
  showLoginBtn() {//启动登录框
    this.setData({
      showModal: true
    })
  },
  preventTouchMove() {//弹出框蒙层截断touchmove事件
  },
  hideModal() {// 隐藏模态对话框
    this.setData({
      showModal: false
    })
  },
  onCancel() {//对话框取消按钮点击事件
    wx.showToast({
      icon: 'none',
      duration: 1000
    })
    this.hideModal()
  },
  onConfirm() {//TODO: 点击登录时 本地缓存 用户名 // 对话框确认按钮点击事件
    let that = this
    if (that.data.Account != null & that.data.Password != null) {
      // console.log(that.data.Account + " , " + that.data.Password)
      // console.log(that.data.Account)
      that.Posting(that.data.Account, that.data.Password)
      that.hideModal()
    } else {
      // console.log(that.data.Account) 
      // console.log(that.data.Account + " , " + that.data.Password)
      return wx.showToast({
        title: '用户名密码不能为空！',
        icon: 'none',
        duration: 1000
      })
    }
  },
  accountChange(e) {// 账户 input 事件 !!! 处理账号值
    let that = this
    that.setData({
      accountSuite: e.detail.value,
    })
  },
  userNameChange(e) {// 账户 input 事件 !!! 处理账号值
    let that = this
    that.setData({
      userName: e.detail.value,
    })
  },
  passwordChange(e) {// 密码 input 
    let that = this
    that.setData({
      passWord: e.detail.value
    })
  },
  deleteBtn(e) {
    let that = this
    that.setData({
    })
    console.log(e.currentTarget.id)
  }
})
